package com.masterclient.app.modules.firstpager.fragments.adapters

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.GridLayout
import android.widget.ImageView
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.masterclient.app.R
import com.eoner.baselibrary.bean.firstpager.PreferentialBean
import com.eoner.baselibrary.utils.GlideUtil
import com.eoner.baselibrary.utils.ScreenUtils
import com.masterclient.app.modules.goods.newpage.activity.NewCommodityDetailActivity
import kotlinx.android.synthetic.main.item_specail_goods.view.*

/**
 * 作者：sean
 * 创建时间：2017/12/15 17:51
 * 功能说明：
 */
class PreferentialAdapter : BaseQuickAdapter<PreferentialBean.DataBean.ShItemsBean, BaseViewHolder> {

    constructor():super(R.layout.item_preferential){
    }

    override fun convert(helper: BaseViewHolder?, item: PreferentialBean.DataBean.ShItemsBean?) {
        GlideUtil.displayImage(mContext,item?.sh_banner,helper?.getView<ImageView>(R.id.iv_banner))
//        App.picasso.load(item?.sh_banner).into(helper?.getView<ImageView>(R.id.iv_banner))

//        if (item.getSh_products) {
        var goodsGridLayout = helper!!.getView<GridLayout>(R.id.gridLayout)
            val productsBeanList = item!!.sh_products
            goodsGridLayout!!.removeAllViews()
            val size = productsBeanList?.size
             Log.i("111",size.toString());
            for (i in 0..(size!!-1)) {
                val productsBean = productsBeanList.get(i)
                val viewItem = View.inflate(mContext, R.layout.item_specail_goods, null)
//                val ivGoods = view.findViewById(R.id.iv_goods) as ImageView
                GlideUtil.displayImage(mContext,productsBeanList[i].sh_image,viewItem.iv_product,R.mipmap.defaults,ScreenUtils.dp2px(100f), ScreenUtils.dp2px(100f))
//                App.picasso
//                        .load(productsBeanList[i].sh_image)
//                        .resize(ScreenUtils.dp2px(100f), ScreenUtils.dp2px(100f))
//                        .placeholder(R.mipmap.defaults)
//                        .error(R.mipmap.defaults)
//                        .into(viewItem.iv_product)
                val params = GridLayout.LayoutParams(ViewGroup.LayoutParams((ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(15f)) / 2, LinearLayout.LayoutParams.WRAP_CONTENT))
                if (i % 2 == 0) {
                    params.setGravity(Gravity.LEFT)
                } else if (i % 2 == 1) {
                    params.setGravity(Gravity.RIGHT)
                }

                params.bottomMargin = ScreenUtils.dp2px(5f)

                viewItem.tv_name.text = productsBean.getSh_name()
//                tvSubTitle.setText(productsBean.getSh_subname())
                viewItem.tv_price_now.text = "¥" + productsBean.sh_show_price

//                if (TextUtils.isEmpty(productsBean.getSh_line_price()) || java.lang.Double.valueOf(productsBean.getSh_line_price())!!.toDouble() == java.lang.Double.valueOf(productsBean.getSh_show_price())!!.toDouble()) {
//                    tvPriceOlder.visibility = View.INVISIBLE
//                } else {
//                    tvPriceOlder.visibility = View.VISIBLE
//                    tvPriceOlder.text = "¥" + productsBean.getSh_line_price()
//                }

                if (0.0 == productsBean.getSh_partner_commission().toDouble()) {
                    viewItem.tv_price_now.visibility = View.VISIBLE
                    viewItem.ll_earn.visibility = View.GONE
                } else {
                    viewItem.tv_price.text = "¥" + productsBean.sh_show_price
                    viewItem.tv_money_earn.text = "赚" + productsBean.sh_partner_commission
                    viewItem.tv_price_now.visibility = View.GONE
                    viewItem.ll_earn.visibility = View.VISIBLE
                }

//                val ivTagNew = view.findViewById(R.id.iv_tag_new) as ImageView
//                val ivShouqing = view.findViewById(R.id.iv_shouqing) as ImageView

//                if ("0" == productsBean.getSh_in_stock()) {
//                    ivShouqing.visibility = View.VISIBLE
//                } else {
//                    ivShouqing.visibility = View.GONE
//                }

                if ("1" == productsBean.getSh_is_new()) {
                    viewItem.iv_tag_new.visibility = View.VISIBLE
                } else {
                    viewItem.iv_tag_new.visibility = View.GONE
                }

//              viewItem.tv_price_o.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG

                /*if (productsBean.getSh_label() != null && !TextUtils.isEmpty(productsBean.getSh_label().getSh_icon_height()) && !TextUtils.isEmpty(productsBean.getSh_label().getSh_icon_width())) {
                    App.picasso.load(productsBean.getSh_label().getSh_icon()).fit().into(ivActivity)
                    ivActivity.visibility = View.VISIBLE
                    val paramsActivity = ivActivity.layoutParams as RelativeLayout.LayoutParams
                    paramsActivity.height = ScreenUtils.dp2px((Integer.valueOf(productsBean.getSh_label().getSh_icon_height())!! / 2).toFloat())
                    paramsActivity.width = ScreenUtils.dp2px((Integer.valueOf(productsBean.getSh_label().getSh_icon_width())!! / 2).toFloat())
                    when (productsBean.getSh_label().getSh_location()) {
                        "left_top" -> paramsActivity.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
                        "right_top" -> paramsActivity.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
                        "left_bottom" -> {
                            paramsActivity.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.iv_goods)
                            paramsActivity.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
                            paramsActivity.setMargins(ScreenUtils.dp2px(7f), 0, 0, ScreenUtils.dp2px(7f))
                        }
                        "right_bottom" -> {
                            paramsActivity.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
                            paramsActivity.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.iv_goods)
                            paramsActivity.setMargins(0, 0, ScreenUtils.dp2px(7f), ScreenUtils.dp2px(7f))
                        }
                    }
                    ivActivity.layoutParams = paramsActivity
                }*/
                viewItem.layoutParams = params
                viewItem.setOnClickListener {
                    var intent = Intent(mContext, NewCommodityDetailActivity::class.java)
                    var bundle = Bundle()
                    bundle.putString("id",productsBean.sh_id)
                    intent.putExtras(bundle)
                    mContext.startActivity(intent)

                }
                goodsGridLayout.addView(viewItem, i)
            }
    }

}